package top.lyjwn.todo.weinxin.api.applet;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.lyjwn.todo.weinxin.api.url.WXAppletUrl;
import top.lyjwn.todo.common.utils.http.HttpUtil;

/**
 * 登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程
 *
 * @author luoyujie
 *
 */
@Component
public class AuthCode2Session {


	@Autowired
	private WXAppletUrl wxAppletUrl;

	public AuthCode2Session() {
	}


	public String getUrl(String code) {
		String url = wxAppletUrl.getAUTH_CODE2_SESSION().replace("#{JSCODE}", code);
		return url;
	}

	public String getResultString(String code) {
		String result = HttpUtil.request(this.getUrl(code), HttpUtil.METHOD_GET);
		return result;
	}

	public JSONObject getResultJSONObject(String code)
	{
		String resultString = this.getResultString(code);
		return JSONObject.parseObject(resultString);
	}

}
